﻿@model IEnumerable<Asset>
@using KPIS.GERP.Models.ICM
@using KPIS.GERP.Models.ICM.ViewModel
@using System.Data.Entity

@{
    ViewBag.Title = "ระบบควบคุมและจำหน่ายพัสดุ";
    ViewBag.Descr = "ทะเบียนควบคุมพัสดุ - ครุภัณฑ์";

    const int ResTypeId = 2;
    Double vmin = 0;
    var resTypeSelectListItems = new List<SelectListItem>();

    var db = new GERPContext();

    List<Asset> modelAssets = db.Assets
        .Include(o => o.AssetStatus)
        .Include(o => o.MasResourceItem)
        .Include(o => o.MasUnit)
        .Include(o => o.Warehouse)
        .Where(o => o.RecordStatus != "D").ToList();

    var movements = db.MovementDtls
        .Include(o => o.Movement)
        .Where(o => o.Movement.RecordStatus != "D").ToList();

    var movementHists = db.MovementHistorys.Where(o => o.RecordStatus != "D").ToList();
    var repairs = db.InventoryRepairs.Where(o => o.RecordStatus != "D").ToList();
    var resourceTypes = db.MasResourceTypes.Where(o => o.RecordStatus != "D").ToList();

    var lookups = db.IcmMasLookUps.ToList();

    resTypeSelectListItems.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

    foreach (var model in resourceTypes)
    {
        var item = new SelectListItem
        {
            Text = model.Name,
            Value = model.ResTypeId.ToString(),
            Selected = (model.ResTypeId == ResTypeId)
        };
        resTypeSelectListItems.Add(item);
    }

    string stringVmin = string.IsNullOrWhiteSpace(Request["vmin"]) ?
        (Session["vmin"] as Double? ?? 10000).ToString("0") : Request["vmin"];

    if (!string.IsNullOrWhiteSpace(Request["comparetype"] as string))
    {
        ViewBag.comparetype = Session["comparetype"] = Request["comparetype"];
    }

    string comparetype = ViewBag.comparetype as string;
    if (string.IsNullOrEmpty(comparetype))
    {
        comparetype = "1";
        ViewBag.comparetype = Session["comparetype"] = comparetype;
    }

    if (!Double.TryParse(stringVmin, out vmin))
    {
        @:กรุณากรอกมูลค่าพัสดุ เป็นตัวเลขเท่านั้น
        <meta http-equiv="refresh" content="2; url=@Url.Action("IndexAsset")/" />
        return;
    }

    Session["vmin"] = vmin;
    Session["comparetype"] = ViewBag.comparetype as string;

    modelAssets = (comparetype == "2" ? modelAssets.Where(o => o.Price < vmin) : modelAssets.Where(o => o.Price >= vmin)).ToList();
}

@using (Html.BeginForm("IndexAsset", "ParcelControlAssetList", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <table>
        <tr>
            <td>
                &nbsp;&nbsp;&nbsp;เลือกประเภทพัสดุ :
            </td>
            <td>
                @Html.DropDownList("ResTypeId", resTypeSelectListItems, new { id = "lenderType", ResTypeId = ResTypeId })
            </td>
            <td>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                ที่มีมูลค่า
                <select id="comparetype" name="comparetype" style="width: 80px">
                    <option value="1" @Html.Raw(comparetype == "1" ? "selected" : "")>>=</option>
                    <option value="2" @Html.Raw(comparetype == "2" ? "selected" : "")><</option>
                </select>
                :
            </td>
            <td>
                <input type="text" id="vmin" style="width: 80px; text-align: right;"
                       value="@vmin.ToString("0.00")" /> บาท
                <input type="button"
                       onclick="window.location='@Url.Action("IndexAsset")?vmin=' + $('#vmin').val() + '&comparetype=' + $('#comparetype').val() "
                       value="แสดง" />
            </td>
        </tr>
    </table>

    <div class="row-fluid">

        @Html.ValidationSummary(true, "", new { @class = "alert alert-error" })

        <div class="row-fluid">
            <div class="table-header span12">
                ทะเบียนควบคุมพัสดุ - ครุภัณฑ์
            </div>
        </div>

        <table id="table_asset" class="table table-striped table-bordered table-hover" aria-describedby="table_asset_info">
            <thead>
                <tr>
                    <th style="width: 110px; text-align: center">วันที่รับเข้า</th>
                    <th style="width: 140px; text-align: center">รหัสสินทรัพย์</th>
                    <th style="width: 140px; text-align: center">รหัสสินทรัพย์ (เก่า)</th>
                    <th style="text-align: center">รายการสินทรัพย์</th>
                    <th style="width: 60px; text-align: center">จำนวน</th>
                    <th style="width: 60px; text-align: center">มูลค่า</th>
                    <th style="width: 80px; text-align: center">สถานที่</th>
                    <th style="width: 60px; text-align: center">สถานะ</th>
                </tr>
            </thead>

            <tbody>
                @foreach (var m in modelAssets.ToList())
                {
                    //var mm = movements.FirstOrDefault(o => o.AssetId == m.AssetId);
                    //var mmh = movementHists.FirstOrDefault(o => o.AssetId == m.AssetId);
                    string assetName = string.IsNullOrWhiteSpace(m.AssetName) ? "-" : m.AssetName;

                    <tr>
                        <td style="text-align: center">
                            <span style="display: none;">
                                @m.AssetDate.ToString("yyyy-MM-dd")
                            </span>
                            @m.AssetDate.ToString("dd MMM yyyy")
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => m.AssetCode)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => m.SerialNumber)
                        </td>
                        <td>
                            @Html.ActionLink(assetName, "AssetDetails", new { m.AssetId })
                            <span style="color: silver">
                                @Html.Raw(string.IsNullOrWhiteSpace(m.Description) ? "" : " - " + m.Description.Replace("\n", "</br>"))
                            </span>
                        </td>
                        <td style="text-align: right">
                            @if (m.MasUnit != null)
                            {
                                <span title="@m.MasUnit.Code">
                                    @Html.DisplayFor(modelItem => m.Quantity) @m.MasUnit.Name
                                </span>
                            }
                            else
                            {
                                @Html.DisplayFor(modelItem => m.Quantity)
                            }
                        </td>
                        <td style="text-align: right">
                            @m.Price.ToString("#,##0.00")
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => m.Warehouse.Name)
                        </td>
                        <td style="text-align: center">
                            @if (m.AssetStatus.LookUpName == "ใช้งานได้")
                            {
                                string lurepaire = "";
                                int repair = 0;

                                foreach (var r in repairs.Where(o => o.AssetCode == m.AssetCode))
                                {
                                    var lup = lookups.FirstOrDefault(o => o.LookUpId == r.RepairStatus);
                                    if (lup != null && lup.LookUpName == "กำลังซ่อมแซม")
                                    {
                                        lurepaire = lup.LookUpName;
                                        repair = r.RepairId;
                                        break;
                                    }
                                }

                                if (!string.IsNullOrEmpty(lurepaire))
                                {
                                    @:@lurepaire
                                    <a href="@Url.Action("Details", "Repair", new {id = repair})" target="_blank">
                                        <i class="icon-external-link"></i>
                                    </a>
                                }
                                else
                                {
                                    @Html.DisplayFor(modelItem => m.AssetStatus.LookUpName)
                                }
                            }
                            else
                            {
                                @Html.DisplayFor(modelItem => m.AssetStatus.LookUpName)
                            }

                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

@section scripts
{
    <script type="text/javascript">
        //var abc = $.QueryString($('#lenderType'));
        //alert(abc);

        $(function () {
            var oTable1 = $('#table_asset').dataTable({
                "aoColumns": [null, null, null, null, null, null, null, null],
                aaSorting: [[0, 'desc']],
                "sDom": '<"top"flp>rt<"bottom"i><"clear">'
            });

            $('table th input:checkbox').on('click', function () {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
                .each(function () {
                    this.checked = that.checked;
                    $(this).closest('tr').toggleClass('selected');
                });

            });
            $('[data-rel=tooltip]').tooltip();
        })

        $(document).ready(function () {
            $('#lenderType').change(function () {
                var resType = parseInt($('#lenderType').val());
                if (resType == null) {
                }
                else if (resType == 1) {
                    window.location.href = "../ParcelControlAssetList/IndexOfficeSuppliesLot";
                }
                //else {
                //    window.location.href = "../ParcelControlAssetList/IndexAsset";
                //}
            });
        });
    </script>
}